package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;
import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.entity.BaseResult;
import com.csweimei.rentmanagementtreasure.http.entity.HouseDetailHzBean;
import com.csweimei.rentmanagementtreasure.http.entity.HouseDetailZzBean;
import com.csweimei.rentmanagementtreasure.pop.PopUtils;
import com.csweimei.rentmanagementtreasure.pop.PopWheelBase;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.customer.ChooseCustomerActivity;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class HzZyVM extends TitleVM {

    public ObservableField<HouseDetailHzBean.DataDTO.HlistDTO> entity = new ObservableField<>();

    public ObservableField<String> housingID = new ObservableField<>();
    public ObservableField<String> customID = new ObservableField<>();

    public ObservableField<String> roomID= new ObservableField<>();


    public ObservableField<String> name= new ObservableField<>();
    public ObservableField<String> contact= new ObservableField<>();
    public ObservableField<String> IDCard= new ObservableField<>();
    public ObservableField<String> nots= new ObservableField<>();



    public ObservableField<String> staTime= new ObservableField<>();
    public ObservableField<String> endTime= new ObservableField<>();
    public ObservableField<String> paymentWay= new ObservableField<>();
    public ObservableField<String> MonthlyRent= new ObservableField<>();
    public ObservableField<String> deposit= new ObservableField<>();

    public ObservableField<String> title= new ObservableField<>();


    public HzZyVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("添加租约");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        getHouseInfo();
    }

    private void getHouseInfo() {
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("sid",housingID.get());
        model.cotenancyinfo(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<HouseDetailHzBean>() {
                    @Override
                    public void onNext(HouseDetailHzBean o) {
                        if (o.issucc){
                            if (ValidateUtils.isNotEmptyObjectOrString(o.getData().getHlist())){
                                entity.set(o.getData().getHlist());

                                for (HouseDetailHzBean.DataDTO.RoomlistDTO bean : o.getData().getRoomlist()) {
                                    if ((bean.getId()+"").equals(roomID.get())){
                                        title.set(entity.get().getRoomname()+"-"+bean.getRoom());
                                    }
                                }

                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public BindingCommand chooseCustomer = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Intent intent = new Intent(mActivity, ChooseCustomerActivity.class);
            intent.putExtra("flag","choose");
            mActivity.startActivityForResult(intent,10086);
        }
    });

    public BindingCommand selStart = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            TimePickerView pvTime = new TimePickerBuilder(mActivity, (date, v) -> {
                staTime.set(new SimpleDateFormat("yyyy-MM-dd").format(date));
            })
                    .setType(new boolean[]{true, true, true, false, false, false})
                    .setLabel("年", "月", "日", "", "", null)
                    .build();
            pvTime.show();
        }
    });

    public BindingCommand selEnd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            TimePickerView pvTime = new TimePickerBuilder(mActivity, (date, v) -> {
                endTime.set(new SimpleDateFormat("yyyy-MM-dd").format(date));
            })
                    .setType(new boolean[]{true, true, true, false, false, false})
                    .setLabel("年", "月", "日", "", "", null)
                    .build();
            pvTime.show();
        }
    });

    public BindingCommand selPayWay = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            PopWheelBase popWheelBase = new PopWheelBase(mActivity, 1, new PopWheelBase.Click() {
                @Override
                public void click(String s) {
                    paymentWay.set(s);
                }
            });
            popWheelBase.setList1(Arrays.asList(mActivity.getResources().getStringArray(R.array.payWayList)));
            PopUtils.showPop(mActivity,popWheelBase,false);
        }
    });


    public BindingCommand saveData = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (ValidateUtils.isEmptyString(name.get())){
                ToastUtils.showShort("请输入租户姓名");
                return;
            }
            if (ValidateUtils.isEmptyString(contact.get())){
                ToastUtils.showShort("请输入租户电话");
                return;
            }

            if (ValidateUtils.isEmptyString(staTime.get())){
                ToastUtils.showShort("请选择后台开始时间");
                return;
            }

            if (ValidateUtils.isEmptyString(endTime.get())){
                ToastUtils.showShort("请选择结束时间");
                return;
            }

            if (ValidateUtils.isEmptyString(paymentWay.get())){
                ToastUtils.showShort("请选择付款方式");
                return;
            }

            if (ValidateUtils.isEmptyString(MonthlyRent.get())){
                ToastUtils.showShort("请输入月租金");
                return;
            }

            if (ValidateUtils.isEmptyString(deposit.get())){
                ToastUtils.showShort("请输入押金");
                return;
            }

            postData();
        }
    });

    private void postData() {
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("housingID",entity.get().getId()+"");
        map.put("customID",customID.get());
        map.put("roomID",roomID.get());
        map.put("name",name.get());
        map.put("contact",contact.get());
        map.put("IDCard",IDCard.get());
        map.put("nots",nots.get());
        map.put("staTime",staTime.get());
        map.put("endTime",endTime.get());
        map.put("paymentWay",paymentWay.get());
        map.put("MonthlyRent",MonthlyRent.get());
        map.put("deposit",deposit.get());

        model.addlease(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult o) {
                        if (o.issucc){
                            ToastUtils.showShort(o.msg);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
